//
//  headView.m
//  xiaoyiNew
//
//  Created by 讯一 on 16/12/26.
//  Copyright © 2016年 讯一. All rights reserved.
//

#import "headView.h"

@interface headView () <UIScrollViewDelegate>

@property (nonatomic,strong)UIScrollView *scroll;
@property (nonatomic,strong)UIPageControl *page;
@property (nonatomic,strong)NSTimer *timer;//循环事件对象

@end

@implementation headView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self.contentView addSubview:self.scroll];
        [self.contentView addSubview:self.page];
        
        NSArray *names = @[@"indexfragementbanner",@"xybanner4",@"indexfragementbanner",@"xybanner4",@"indexfragementbanner",@"xybanner4"];//增加第0页和最后一页
        for (int i = 0; i < names.count; i ++) {
            UIImage *image = [UIImage imageNamed:names[i]];//获取图片
            
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(i * Screen_Width , 0, Screen_Width, self.frame.size.height)];
            imageView.image = image;
            [_scroll addSubview:imageView];//这里是用scroll添加
    }
    }
    return self;
}

- (UIScrollView *)scroll
{
    if (!_scroll) {
        _scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0,Screen_Width, self.bounds.size.height)];
        //_scroll.center = self.contentView.center;
        _scroll.pagingEnabled = YES;//按页滑动******
        _scroll.showsHorizontalScrollIndicator = NO;//开启下方滚动条****
        _scroll.bounces = NO;//关闭滑过最边上*****(防止弹动)
        _scroll.delegate = self;//设置滑动代理（监听）
        
        _scroll.contentOffset = CGPointMake(Screen_Width, 0);//使得载入时出现的是第一页
        
        //滑动区域的高和宽
        _scroll.contentSize = CGSizeMake(7*Screen_Width, 0);//表示只能横向滑动*********
    }
    return _scroll;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger index = _scroll.contentOffset.x / Screen_Width;
    if (index < 1) {
        _scroll.contentOffset  = CGPointMake(5 * Screen_Width, 0);//切换到第四页
    }else if (index > 5){
        _scroll.contentOffset = CGPointMake(Screen_Width, 0);//切换到第一页
    }
    NSInteger newIndex = _scroll.contentOffset.x/Screen_Width;
    _page.currentPage = newIndex-1;
}
- (UIPageControl *)page{
    if (!_page) {
        //pageControl按页滑动视图(显示下方小白圆点)
        _page = [[UIPageControl alloc]initWithFrame:CGRectMake(0, self.bounds.size.height - 35, Screen_Width, 30)];
        _page.numberOfPages = 5;
        _page.pageIndicatorTintColor = [UIColor whiteColor];
        _page.currentPageIndicatorTintColor = TSColor(1, 199, 166);
    }
    return _page;
}


- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    [self scrollViewDidEndDecelerating:scrollView];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [_timer invalidate];//终止
    _timer = nil;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    //添加自动轮播
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    
}


-(void)nextPage{
    [_scroll setContentOffset:CGPointMake(_scroll.contentOffset.x + Screen_Width, 0) animated:YES];
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
